#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main(){
    string b = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string a;
    int A,q,r=0;
    cin >> a >> A;
    for (int i =0;i<a.length();i++){
        char h=a[i];
        string str(1, h);
        q=b.find(h);
        r+=q*pow(A,a.length()-1-i);
    }
    cout << r << endl;
}
